home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / sthing.com / STHING.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-10-28  |  8.9 KB  |  349 lines

  1. {
  2.   STHING.PAS. Turbo Pascal interface to Covox Speech Thing. See STHING.DOC for
  3.   further information.
  4.  
  5.   Written 10/90, Kim Kokkonen, TurboPower Software
  6.   Copyright (C) 1990, TurboPower Software. All rights reserved.
  7. }
  8.  
  9. {$R-,S-,I-,V-,B-,F+,O-,A-}
  10.  
  11. unit Sthing;
  12.   {-Direct interface to SPEECHV2 or SPEECHV2}
  13.  
  14. interface
  15.  
  16. const
  17.   StAllocFromHeap : Boolean = True;
  18.   {Set to False if TP program will leave DOS memory free for
  19.    allocation of words stored in dictionary.}
  20.  
  21. {----------------  basic functions  -------------------------------}
  22.  
  23. function StLoaded : Boolean;
  24.   {-Returns True if SPEECHVx is loaded}
  25.  
  26. procedure StSetPort(Port : Word);
  27.   {-Set peripheral port where speaker output goes. Default to LPT1 port}
  28.  
  29. procedure StSetLptPort(LPTNumber : Byte);
  30.   {-Set peripheral port for LPT 1, 2, or 3}
  31.  
  32. procedure StSpeak(St : string);
  33.   {-Speak the specified english string}
  34.  
  35. procedure StSetParams(Tone, Volume, Pitch, Speed : Word);
  36.   {-Set parameters}
  37.   {   param  valid range  default
  38.       ------ -----------  -------
  39.       Tone     0..1          0
  40.       Volume   0..9          5
  41.       Pitch    0..9          5
  42.       Speed    0..9          5
  43.    StSetParams does not range-check the values it receives.
  44.   }
  45.  
  46. procedure StGetParams(var Tone, Volume, Pitch, Speed : Word);
  47.   {-Get the last parameters set}
  48.  
  49. procedure StGrabInt7E;
  50.   {-Modify abort-speech-on-keypressed behavior: SPEECHV3 only}
  51.  
  52. procedure StRestoreInt7E;
  53.   {-Restore abort-speech-on-keypressed behavior}
  54.  
  55. procedure StUnload;
  56.   {-Unload SPEECHV2 or SPEECHV3 from memory}
  57.  
  58. {----------------  phonetic speech functions  ---------------------}
  59.  
  60. procedure StTextToPhonetic(TextSt : string; var PhonSt : string);
  61.   {-Convert text string to phonetic}
  62.  
  63. procedure StPhoneticSpeak(St : string);
  64.   {-Speak the specified phonetic string}
  65.  
  66. {----------------  dictionary functions  --------------------------}
  67.  
  68. procedure StInitDict(Clear : Boolean);
  69.   {-Reset the dictionary. Clear = True wipes all entries}
  70.  
  71. function StInsertDict(TextSt : string; PhonSt : string) : Boolean;
  72.   {-Insert a new entry in dictionary. Returns False if insuff memory}
  73.  
  74. procedure StRemoveDict(TextSt : string);
  75.   {-Remove an entry previously added to dictionary}
  76.   {Note: when StAllocFromHeap is True, heap space is NOT reclaimed}
  77.  
  78. procedure StDumpDict(var TextSt : string; var PhonSt : string);
  79.   {-Call repeatedly to get dictionary entries}
  80.   {Example:
  81.      StInitDict(False);
  82.      StDumpDict(TextSt, PhonSt);
  83.      while TextSt <> '' do begin
  84.        writeln(TextSt:40, PhonSt:40);
  85.        StDumpDict(TextSt, PhonSt);
  86.      end;
  87.   }
  88.  
  89. function StWriteDictFile(FName : string) : Word;
  90.   {-Write dictionary to text file, returning status}
  91.  
  92. function StReadDictFile(FName : string) : Word;
  93.   {-Read dictionary from text file, returning status}
  94.  
  95. const
  96.   {Interesting stats for dictionary memory allocation}
  97.   AlloCnt : LongInt = 0; {Number of allocations}
  98.   ParaReq : LongInt = 0; {Paragraphs requested}
  99.   BumpCnt : LongInt = 0; {Number of times we had to bump the para count}
  100.  
  101.   {====================================================================}
  102.  
  103. implementation
  104.  
  105. var
  106.   SpeechPtr : pointer;              {Entry pointer for SPEECHVx}
  107.   lUnknown : Word;                  {May be 0 or 1. Has no discernable effect}
  108.   lTone : Word;                     {Last values for tone, etc.}
  109.   lVolume : Word;
  110.   lPitch : Word;
  111.   lSpeed : Word;
  112.   Int21Err : Word;                  {Error for dict memory allocation}
  113.   SaveExit : Pointer;
  114.   Have21 : Boolean;
  115.   Have7E : Boolean;
  116.   Loaded : Boolean;                 {True if SPEECHVx loaded}
  117.   SaveSP : Word;                    {Keep SPEECHVx from corrupting stack}
  118.  
  119. function StLoaded : Boolean;
  120. begin
  121.   StLoaded := Loaded;
  122. end;
  123.  
  124. {$L STHING.OBJ}
  125. procedure StInit; external;
  126. procedure StSetPort(Port : Word); external;
  127. procedure StSetLptPort(LPTNumber : Byte); external;
  128. procedure StSetParams(Tone, Volume, Pitch, Speed : Word); external;
  129. procedure StSpeak(St : string); external;
  130. procedure StTextToPhonetic(TextSt : string; var PhonSt : string); external;
  131. procedure StPhoneticSpeak(St : string); external;
  132. procedure StInitDict(Clear : Boolean); external;
  133. function StInsertDict(TextSt : string; PhonSt : string) : Boolean; external;
  134. procedure StRemoveDict(TextSt : string); external;
  135. procedure StDumpDict(var TextSt : string; var PhonSt : string); external;
  136. procedure StGrabInt7E; external;
  137. procedure StRestoreInt7E; external;
  138. procedure StGrabInt21; external;
  139. procedure StRestoreInt21; external;
  140. procedure StUnload; external;
  141.  
  142. {$F-}
  143. function AllocSeg(Paras : Word) : Word;
  144.   {-Paragraph allocator called from assembly language}
  145. type
  146.   OS = record O, S : Word end;
  147. var
  148.   Avail : LongInt;
  149.   Bytes : Word;
  150.   P : Pointer;
  151. begin
  152.   inc(AlloCnt);
  153.   inc(ParaReq, Paras);
  154.   AllocSeg := 0;
  155.   if Paras <= $0FFF then begin
  156.     Bytes := Paras shl 4;
  157.     Avail := MemAvail;
  158.     if Avail >= Bytes then begin
  159.       GetMem(P, Bytes);
  160.       if OS(P).O = 0 then
  161.         AllocSeg := OS(P).S
  162.       else begin
  163.         FreeMem(P, Bytes);
  164.         inc(Bytes, 15);
  165.         inc(BumpCnt);
  166.         if Avail >= Bytes then begin
  167.           GetMem(P, Bytes);
  168.           if OS(P).O = 0 then
  169.             AllocSeg := OS(P).S
  170.           else
  171.             AllocSeg := OS(P).S+1;
  172.         end;
  173.       end;
  174.     end;
  175.   end;
  176. end;
  177. {$F+}
  178.  
  179. procedure StGetParams(var Tone, Volume, Pitch, Speed : Word);
  180. begin
  181.   Tone := lTone;
  182.   Volume := lVolume;
  183.   Pitch := lPitch;
  184.   Speed := lSpeed;
  185. end;
  186.  
  187. procedure Pad(var S : String; Len : Byte);
  188. var
  189.   SLen : byte absolute S;
  190. begin
  191.   if SLen >= Len then
  192.     S := S+' '
  193.   else begin
  194.     FillChar(S[SLen+1], Len-SLen, ' ');
  195.     SLen := Len;
  196.   end;
  197. end;
  198.  
  199. function StWriteDictFile(FName : string) : Word;
  200. var
  201.   Status : word;
  202.   TextSt : string;
  203.   PhonSt : string;
  204.   F : text;
  205. begin
  206.   if not Loaded then begin
  207.     StWriteDictFile := $FFFF;
  208.     Exit;
  209.   end;
  210.  
  211.   assign(F, FName);
  212.   rewrite(F);
  213.   Status := IoResult;
  214.   if Status <> 0 then begin
  215.     StWriteDictFile := Status;
  216.     Exit;
  217.   end;
  218.  
  219.   StInitDict(False);
  220.   StDumpDict(TextSt, PhonSt);
  221.   while TextSt <> '' do begin
  222.     Pad(TextSt, 32);
  223.     writeln(F, TextSt, PhonSt);
  224.     Status := IoResult;
  225.     if Status <> 0 then begin
  226.       StWriteDictFile := Status;
  227.       close(F);
  228.       Status := IoResult;
  229.       Exit;
  230.     end;
  231.     StDumpDict(TextSt, PhonSt);
  232.   end;
  233.  
  234.   close(F);
  235.   StWriteDictFile := IoResult;
  236. end;
  237.  
  238. function StReadDictFile(FName : string) : Word;
  239. var
  240.   Status : word;
  241.   BPos : byte;
  242.   CPos : byte;
  243.   St : string;
  244.   F : text;
  245. begin
  246.   if not Loaded then begin
  247.     StReadDictFile := $FFFF;
  248.     Exit;
  249.   end;
  250.  
  251.   assign(F, FName);
  252.   reset(F);
  253.   Status := IoResult;
  254.   if Status <> 0 then begin
  255.     StReadDictFile := Status;
  256.     Exit;
  257.   end;
  258.  
  259.   while not Eof(F) do begin
  260.     ReadLn(F, St);
  261.     if Length(St) <> 0 then
  262.       if St[1] <> ';' then begin
  263.         BPos := pos(' ', St);
  264.         if BPos <> 0 then begin
  265.           CPos := BPos;
  266.           while (CPos <= Length(St)) and (St[CPos] = ' ') do
  267.             inc(CPos);
  268.           if CPos <= Length(St) then begin
  269.             if not StInsertDict(Copy(St, 1, BPos-1),
  270.                                 Copy(St, CPos, Length(St)-CPos+1))
  271.             then begin
  272.               StReadDictFile := 8;
  273.               Close(F);
  274.               Status := IoResult;
  275.               Exit;
  276.             end;
  277.           end else begin
  278.             StReadDictFile := 106;
  279.             Close(F);
  280.             Status := IoResult;
  281.             Exit;
  282.           end;
  283.         end else begin
  284.           StReadDictFile := 106;
  285.           Close(F);
  286.           Status := IoResult;
  287.           Exit;
  288.         end;
  289.       end;
  290.   end;
  291.   close(F);
  292.   StReadDictFile := IoResult;
  293. end;
  294.  
  295. procedure FindSpeech;
  296. const
  297.   MinIntr = $60;
  298.   MaxIntr = $FD;
  299.   IntsToCheck : set of MinIntr..MaxIntr =
  300.     {Interrupts that SPEECHV2 or SPEECHV3 may take over on various machines}
  301.     [$60..$67, $F1..$F7, $F9, $FC..$FD];
  302. var
  303.   Vectors : array[0..$FF] of pointer absolute $0:$0;
  304.   Intr : Byte;
  305. begin
  306.   for Intr := MinIntr to MaxIntr do
  307.     if Intr in IntsToCheck then begin
  308.       SpeechPtr := Vectors[Intr];
  309.       if LongInt(SpeechPtr^) = $FB3C0B3C then begin
  310.         {Skip over dummy instructions}
  311.         inc(LongInt(SpeechPtr), 4);
  312.         Loaded := True;
  313.         Exit;
  314.       end;
  315.     end;
  316.   Loaded := False;
  317. end;
  318.  
  319. procedure StExit;
  320. begin
  321.   ExitProc := SaveExit;
  322.   StRestoreInt7E;
  323.   StRestoreInt21; {Checks to see if Have21 is True}
  324. end;
  325.  
  326. begin
  327.   FindSpeech;
  328.   if Loaded then begin
  329.     {Set up for default LPT1 port}
  330.     StSetPort(0);
  331.  
  332.     {Initialize SPEECHV2 or SPEECHV3}
  333.     StInit;
  334.  
  335.     {Set default speech parameters}
  336.     lUnknown := 0;
  337.     StSetParams(0, 5, 5, 5);
  338.  
  339.     {Take over int 7E, which determines when to throw away keys}
  340.     Have7E := False;
  341.     StGrabInt7E;
  342.  
  343.     {Set up exit handler to restore vectors in case of error}
  344.     Have21 := False;
  345.     SaveExit := ExitProc;
  346.     ExitProc := @StExit;
  347.   end;
  348. end.
  349.